package lanqiao.真题.第13届Java大学B组;

import java.io.*;
import java.util.Arrays;
import java.util.StringTokenizer;

/**
 * @author 挚爱之夕
 * @version 1.0
 * @implSpec
 * 小蓝老师教的编程课有 N 名学生，编号依次是 1 . . . N。第 i 号学生这学期刷题的数量是 Ai。
 * 对于每一名学生，请你计算他至少还要再刷多少道题，才能使得全班刷题比他多的学生数不超过刷题比他少的学生数。
 * 样例输入
 * 5
 * 12 10 15 20 6
 * 样例输出
 * 0 3 0 0 7
 * 对于30%的数据，1≤N≤1000,0≤Ai≤1000.
 * 对于100%的数据，1≤N≤100000,0≤Ai≤100000.
 * 思路一：中位数
 * 思路二：二分 + 前缀和
 * @since 2023 - 03 - 07 - 18:05
 */
public class D_最小刷题数 {
}

/*
二分 + 前缀和
二分 找每个学生满足情况的最小刷题数
前缀和 方便查询刷题段有多少学生
 */
class Main_D{
    static int MAX = 100000;
    static int[] nums = new int[100005];
    static int[] cnt = new int[100005];
    static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    static PrintWriter writer = new PrintWriter(new OutputStreamWriter(System.out));
    static StringTokenizer tokenizer = new StringTokenizer("");
    static String nextLine() throws IOException {
        return reader.readLine();
    }
    static int nextInt() throws IOException {
        if(!tokenizer.hasMoreTokens()){
            tokenizer = new StringTokenizer(nextLine());
        }
        return Integer.parseInt(tokenizer.nextToken());
    }

    public static void main(String[] args) throws IOException {
        int n = nextInt();
        for(int i = 0; i < n; i++){
            nums[i] = nextInt();
            //统计每个刷题数的人数
            cnt[nums[i]]++;
        }
        //构造前缀和数组
        for(int i = 1; i <= MAX; i++){
            cnt[i] += cnt[i - 1];
        }
        for(int i = 0; i < n; i++){
            //该同学不需要刷题
            //nums[i] 可能为0, 如果是取0
            if(cnt[MAX] - cnt[nums[i]] <= cnt[Math.max(0, nums[i] - 1)]){
                writer.print("0 ");
                continue;
            }
            //需要刷题，找最小刷题数
            int l = nums[i] + 1, r = MAX, mid;
            while(l < r){
                mid = l + ((r - l) >> 1);
                /*
                为什么还需要减去1？
                因为这位同学原先的刷题数是小于x的， 此时他已经变为x了，所以统计比他少刷题数的同学时需要把他去掉。
                 */
                if(cnt[MAX] - cnt[mid] <= cnt[mid - 1] - 1){
                    r = mid;
                }else{
                    l = mid + 1;
                }
            }
            writer.print((r - nums[i]) + " ");
        }
        writer.flush();
    }
}

/*
中位数
 */
class Main_D2 {
    /**
     * 大数据量输入不能使用scanner 会超时
     */
    static BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
    //字符串解析器
    static StringTokenizer stringTokenizer = new StringTokenizer("");

    public static String nextLine() throws IOException {
        return bufferedReader.readLine();
    }

    public static String next() throws IOException {
        while (!stringTokenizer.hasMoreElements()) {
            stringTokenizer = new StringTokenizer(nextLine());
        }
        return stringTokenizer.nextToken();
    }

    public static int nextInt() throws NumberFormatException, IOException {
        return Integer.parseInt(next());
    }

    public static void main(String[] args) throws NumberFormatException, IOException {
        int n = nextInt();
        int[] nums = new int[n];
        //排序
        int[] helper = new int[n];
        for (int i = 0; i < n; i++) {
            nums[i] = nextInt();
            helper[i] = nums[i];
        }
        //中间数
        int mid = n / 2;
        Arrays.sort(helper);
        int val = helper[mid];
        //左右两边的数个数
        int leftNum = 0;
        int rightNum = 0;
        for (int i = 0; i < n; i++) {
            if (helper[i] < val) {
                leftNum++;
            } else if (helper[i] > val) {
                rightNum++;
            }
        }
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < n; i++) {
            //不需要刷题的情况
            if (nums[i] > val || (nums[i] == val && leftNum >= rightNum)) {
                stringBuilder.append(0).append(" ");
                continue;
            }
            //需要刷到比中位数大1
            if (leftNum < rightNum || (leftNum == rightNum && nums[i] < val)) {
                stringBuilder.append(val + 1 - nums[i]).append(" ");
                continue;
            }

            stringBuilder.append(val - nums[i]).append(" ");
//            if(leftNum == rightNum - 1){
//                stringBuilder.append(val - nums[i]).append(" ");
//            }else{
//                stringBuilder.append(val + 1 - nums[i]).append(" ");
//            }

        }

        String answer = stringBuilder.substring(0, stringBuilder.length() - 1);
        System.out.println(answer);
    }
}